package com.wd.mine.activity

import android.widget.Toast
import androidx.activity.viewModels
import com.blankj.utilcode.util.SPUtils
import com.wd.common.base.BaseActivity
import com.wd.common.viewmodel.MyViewModel
import com.wd.mine.databinding.ActivityMeLoginBinding
import org.greenrobot.eventbus.EventBus


class MeLoginActivity : BaseActivity<ActivityMeLoginBinding, Any?>() {
    override fun getViewBinding() = ActivityMeLoginBinding.inflate(layoutInflater)
    val viewModel: MyViewModel by viewModels()

    override fun initData() {
        binding.login.setOnClickListener{
            val userName = binding.userName.text.toString()
            val passwd = binding.passwd.text.toString()
            if(userName != "" && passwd != ""){
                viewModel.postUserLogin(userName, passwd)
                viewModel.userLoginData.observe(this){
                    if(it.message == "查询成功"){
                        SPUtils.getInstance().put("userId",it.result?.userId!!)
                        SPUtils.getInstance().put("token",it.result?.token!!)
                        SPUtils.getInstance().put("headPic",it.result?.headPic!!)
                        EventBus.getDefault().post(it)
                    }
                    Toast.makeText(this,it.message,Toast.LENGTH_SHORT).show()
                }
            }
        }

    }

}